<?php
/**
 * スマーティセレクトメニュークラスモジュール
 */
require_once dirname(__FILE__) . '/Components.php';
require_once 'Request/Request.php';

class Smarty_Components_Select extends Smarty_Components
{
	/** テンプレートファイル名 */
	const TEMPLATE_NAME = 'select.tpl';

	/** SELECT NAME */
	protected $_sName;
	/** OPTIONS */
	protected $_aryOptions;
	/** SELECTED */
	protected $_sSelected;
	/** ID */
	protected $_ID;

	/**
	 * コンストラクタ
	 * Enter description here ...
	 * @param unknown_type $sCompileDir
	 * @param unknown_type $sCompileId
	 */
	public function __construct($sCompileDir, $sCompileId, $sName, $aryOptions, $Id='')
	{
		parent::__construct(dirname(__FILE__), $sCompileDir, $sCompileId);
		$this->_sName = $sName;
		$this->_aryOptions = $aryOptions;
		$this->_ID =$Id;
		$this->_setSelectedValue();
	}

	/**
	 * 選択状態変更jsメソッド名を返す
	 * selectedChange<?$ID?>
	 */
	public function getNameJsFunction()
	{
		return 'selectedChange'.$this->_ID;
	}

	/**
	 * 選択状態変更jsメソッド呼び出しステップ文字列を返す
	 * "selectedChange('SaleItemId', 55)"
	 */
	public function getCallWordJsFunction($SelectValue, $bDcReturn=TRUE)
	{
		$sMethodName = $this->getNameJsFunction();
		$sFirstParam = $this->_ID;
		$sResult =  $sMethodName . "('" . $sFirstParam . "', " . "'" . $SelectValue . "')";
		if ($bDcReturn == TRUE) {
			$sResult = '"' . $sResult . '"';
		}
		return $sResult;
	}

	/**
	 * FETCH(non-PHPdoc)
	 * @see Smarty_Components::fetch()
	 */
	public function fetch()
	{
		$this->view->SELECTED = $this->_sSelected;
		$this->view->NAME = $this->_sName;
		$this->view->OPTIONS = $this->_aryOptions;
		$this->view->ID = $this->_ID;
		$sHtml = parent::fetch(self::TEMPLATE_NAME);
		return $sHtml;
	}

	/**
	 * 選択初期値設定
	 * @param string|array $Values 設定値
	 */
	public function setSelectedValue($Values)
	{
		$value = $Values;
		if ( is_array($Values) ) {
			$value = $Values[0];
		}
		$this->_sSelected = $value;
	}

	/**
	 * HTTPリクエストから選択初期値を取得する
	 * Enter description here ...
	 */
	protected function _setSelectedValue()
	{
		$objRequest = new Lib_Request();
		$value = $objRequest->get($this->_sName);
		$this->setSelectedValue($value);
	}
}